package com.woniuxy.creditsystem.moneyditchmag.controller;


import com.github.pagehelper.PageInfo;
import com.woniuxy.creditsystem.common.bean.*;
import com.woniuxy.creditsystem.common.util.TraceUtil;
import com.woniuxy.creditsystem.moneyditchmag.service.IMoneyDitch;
import com.woniuxy.creditsystem.productmag.service.IProduct;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@RequestMapping("/monditch")
@RestController
public class MoneyDitchController {


    @Resource
    private IMoneyDitch iMoneyDitch;



    @GetMapping(value="/all",produces = {"application/json;charset=utf-8"})
    public List<MoneyChannelInfo> findDitchIdAndDitchAllname(){

        List<MoneyChannelInfo> DitchId = iMoneyDitch.findDitchIdAndDitchAllName();
        return DitchId;
    }

    /**
     * 分页
     * @param request
     * @param moneyChannelInfo
     * @return
     */
    @GetMapping(value="/page",produces = {"application/json;charset=utf-8"})
    public TableResult<MoneyChannelInfo> findAllByParams(PageRequest request, MoneyChannelInfo moneyChannelInfo){

        //System.out.println("-----------------------------");

        //System.out.println("到资金渠道太代码了！！");


        PageInfo<MoneyChannelInfo> pageInfo = iMoneyDitch.findAllByParams(request,moneyChannelInfo);
        return   new TableResult<MoneyChannelInfo>(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 新增
     * @param moneyChannelInfo
     * @return
     */
    @PostMapping(value = "/more",produces = {"application/json;charset=utf-8"})
    public ResultMsg saveProduct(MoneyChannelInfo moneyChannelInfo){
//        System.out.println("俺到后台了！");
//        System.out.println(productInfo.getProductDescp());

        System.out.println("进入新增功能后台控制层");
        String productNums = new Date().getTime()+"";

        String status = "已禁用";

        moneyChannelInfo.setStatus(status);


        moneyChannelInfo.setDitchNums(System.currentTimeMillis());;
        System.out.println("前端传进去的值="+moneyChannelInfo);

        iMoneyDitch.savaMoneyChannelInfo(moneyChannelInfo);

        ResultMsg resultMsg = new ResultMsg(200,"新增成功",null);

        return resultMsg;
    }


    @PutMapping(value="/{id}",produces = {"application/json;charset=utf-8"})
    public ResultMsg updaProductInfoById(MoneyChannelInfo moneyChannelInfo, @PathVariable("id") Long id){
//        RoleInfo roleInfo=new RoleInfo();
//        roleInfo.setId(roleInfo_id);
//        userInfo.setRoleInfo(roleInfo);

        //System.out.println("我到修改后端了");

        //System.out.println("我到修改的地方了");



        ResultMsg resultMsg = new ResultMsg(200,"修改成功",null);
        iMoneyDitch.updaMoneyChannelInfo(moneyChannelInfo);

        return resultMsg;
    }

    @PutMapping(value = "/upsta",produces = {"application/json;charset=utf-8"})
    public  ResultMsg updaStatusById(MoneyChannelInfo moneyChannelInfo){
        System.out.println(moneyChannelInfo.getId());
        System.out.println(moneyChannelInfo.getStatus());
        System.out.println("我到更改状态的后台了");
        ResultMsg resultMsg = new ResultMsg(200,"修改成功",null);
        iMoneyDitch.updaStatusById(moneyChannelInfo);

        return resultMsg;
    }


    /**
     * 把前台传回来的table中的 list集合 导出到一个Excel文件
     * @author
     * @param tableDataJson
     * @return ResultMsg
     */
    @PostMapping(value="/exportList",produces = {"application/json;charset=utf-8"})
    public com.woniuxy.creditsystem.common.util.ResultMsg tableDataJson(@RequestBody List<MoneyChannelInfo> tableDataJson) throws IOException {
        iMoneyDitch.write2excel(tableDataJson, "D:/excel/output");



        System.out.println("\n" + TraceUtil.positionTrace());
        for (MoneyChannelInfo moneyChannelInfo : tableDataJson) {
            System.out.println(moneyChannelInfo);
        }
        com.woniuxy.creditsystem.common.util.ResultMsg resultMsg = new com.woniuxy.creditsystem.common.util.ResultMsg(200, "in tableDataJson()", null);
        return resultMsg;


    }

    }
